﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace ggprot
{
    public class GGRecvMsg : GGHeader
    {
        private int sender;
        private int seq;
        private int time;
        private int cls;
        private string message;
        private int plainOffset;
        private int attribOffset;
        private string htmlMessage;
        private byte[] attrib;

        public byte[] Attrib
        {
            get { return attrib; }
            set { attrib = value; }
        }

        public string HtmlMessage
        {
            get { return htmlMessage; }
            set { htmlMessage = value; }
        }

        public int AttribOffset
        {
            get { return attribOffset; }
            set { attribOffset = value; }
        }

        public int PlainOffset
        {
            get { return plainOffset; }
            set { plainOffset = value; }
        }

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public int Sender
        {
            get { return sender; }
            set { sender = value; }
        }

        public int Seq
        {
            get { return seq; }
            set { seq = value; }
        }

        public int Cls
        {
            get { return cls; }
            set { cls = value; }
        }

        public int Time
        {
            get { return time; }
            set { time = value; }
        }

        public GGRecvMsg(GGHeader header)
        {
            this.Type = header.Type;
            this.Length = header.Length;
        }

        public override void Read(NetworkStream stream)
        {
            BinaryReader reader = new BinaryReader(stream, Encoding.UTF8);

            sender = reader.ReadInt32();
            seq = reader.ReadInt32();
            time = reader.ReadInt32();
            cls = reader.ReadInt32();
            plainOffset = reader.ReadInt32();
            attribOffset = reader.ReadInt32();
            htmlMessage = String.Concat(reader.ReadChars(plainOffset - 24));
            message = String.Concat(reader.ReadChars(attribOffset - plainOffset));
            attrib = reader.ReadBytes(Length - attribOffset);
        }
    }
}
